//
//  PosterVCollectionView.m
//  Mtime
//
//  Created by Mac on 15/8/1.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "PosterVCollectionView.h"
#import "PosterCell.h"

@implementation PosterVCollectionView

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        self.itemWidth = kScreenW -100;
        
//        [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        //当子类化出cell文件时  注意更改单元格的类
        [self registerClass:[PosterCell class] forCellWithReuseIdentifier:@"cell"];
    }

    return self;
}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PosterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.transform = CGAffineTransformMakeScale(.9, .9);
    
    cell.model = self.dataList[indexPath.item];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.currentIndexPath == indexPath) {//位置一样  实现翻转
        
        //cellForItemAtIndexPath:获得选中的单元格
        PosterCell *cell = (PosterCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        [cell flipAction];

    }else {
    
        
        [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
        //给当前单元格的位置属性重新赋值
        self.currentIndexPath = indexPath;
    }
    
    
    
}

//当单元格结束显示／放回复用池的时候调用
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

    PosterCell *posterCell = (PosterCell *)cell;

    [posterCell bringAction];

}

@end
